//
//  UIViewController+Extension.swift
//  BaoKeSwift
//
//  Created by geek-duan on 2017/10/12.
//  Copyright © 2017年 geek-duan. All rights reserved.
//

import Foundation
import UIKit

extension UIViewController {
    
    // MARK: - 获取当前view的视图控制器
    func getCurrentVC() -> UIViewController{
        let result: UIViewController
        var window = UIApplication.shared.keyWindow
        if window?.windowLevel != UIWindowLevelNormal {
            let windows = UIApplication.shared.windows
            for tmpWin in windows {
                if tmpWin.windowLevel == UIWindowLevelNormal {
                    window = tmpWin
                    break
                }
            }
        }
        let frontView = window?.subviews[0]
        let nextResponder = frontView?.next
        if (nextResponder?.isKind(of: UIViewController.self as AnyClass))! {
            result = nextResponder as! UIViewController
        } else {
            result = (window?.rootViewController)!
        }
        return result
    }
    
    // MARK: - 获取几个主要视图控制器（如：首页）；注意：项目框架要为 tabBarVC.rootVC = NavVC, NavVC.rootVC = homeVC
    func getMainViewCOntroller(index: Int) -> UIViewController {
        
        let rootViewController = UIApplication.shared.keyWindow?.rootViewController
        let mainViewController = rootViewController?.childViewControllers[index].childViewControllers[0]
        return mainViewController ?? UIViewController()
    }
}
